package com.kun.controller;

import com.kun.common.Constants;
import com.kun.common.Result;
import com.kun.controller.dto.MenuDto;
import com.kun.entity.Menu;
import com.kun.exception.MyServiceException;
import com.kun.service.MenuServiceImpl;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    private MenuServiceImpl menuService;


    @GetMapping("/deleteMenu")
    @ApiOperation("删除菜单")
    @ResponseBody
    public Result deleteMenu(@RequestParam("id")int id){
        menuService.deleteMenu(id);
        return  Result.success();
    }

    @PostMapping("/addMenu")
    @ApiOperation("新增菜单")
    @ResponseBody
    public Result addMenu(@RequestBody Menu menu){
        menuService.addMenu(menu);
        return  Result.success();
    }

    @PostMapping("/updateMenuState")
    @ApiOperation("更新菜单状态")
    @ResponseBody
    public Result updateMenuState(@RequestBody List<Integer> menuIds){
        ArrayList<Menu> menus = new ArrayList<>();
        for (Integer menuId : menuIds) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("id",menuId);
            List<Menu> menusTempData = menuService.selectAllMenus(map);
            menus.add(menusTempData.get(0));
        }
        return  Result.success(menus);
    }

    @PostMapping("/batchDeleteMenu")
    @ApiOperation("批量删除菜单")
    @ResponseBody
    public Result batchDeleteMenu(@RequestBody List<Integer> ids){
        for (Integer id : ids) {
            this.deleteMenu(id);
        }
        return  Result.success();
    }


    @GetMapping("/queryAllMenus")
    @ApiOperation("查询所有的菜单")
    @ResponseBody
    public Result queryAllMenus(@ApiParam("当前页数") @RequestParam(value = "pageIndex",required = false)Integer pageIndex,
                                @ApiParam("页面大小") @RequestParam(value ="pageSize",required = false) Integer pageSize,
                                @ApiParam("菜单名") @RequestParam(value = "menuName",required = false) String menuName){

        HashMap<String, Object> map = new HashMap<>();
        if (pageIndex!=null || pageSize!=null){
            map.put("pageIndex",pageIndex);
            map.put("pageSize",pageSize);
        }
        map.put("menuName_like",menuName);
        List<Menu> menus = menuService.selectAllMenus(map);
        ArrayList<MenuDto> menuDtos = new ArrayList<>();
        for (Menu menu : menus) {
            if (menu.getParentId()==null){
                menuDtos.add(new MenuDto(menu));
            }else{
                Integer parentId = menu.getParentId();
                HashMap<String, Object> map1 = new HashMap<>();
                map1.put("id",menu.getParentId());
                if (menuService.selectAllMenus(map1).size()!=1){
                    throw new MyServiceException(Constants.CODE_600,"数据库异常");
                }else{
                    for (MenuDto menuDto : menuDtos) {
                        if (menuDto.getId() == parentId){
                            menuDto.getChildren().add(menu);
                            break;
                        }
                    }
                }
            }
        }
        return  Result.success(menuDtos);
    }

    @GetMapping("/queryAllMenusNum")
    @ApiOperation("查询菜单总数")
    @ResponseBody
    public Result queryAllMenusNum(@ApiParam("文件名") @RequestParam(value = "menuName",required = false) String menuName){
        HashMap<String, Object> map = new HashMap<>();
        map.put("menuName_like",menuName);
        List<Menu> menus = menuService.selectAllMenus(map);
        return  Result.success(menus.size());
    }










}
